banner

Working With Functions (Continues)

OPENING QUESTION: Take a look at this code: The "main program" and the "function". What happens when MAIN is run?

"Main" Program
Function

 

It's a little bit weird (I intentionally named it something non-sensical, please don't do that when you're coding), but see if you can identify how I'm *sending* information into the function...

 

LEARNING TARGET: I will SUCCESSFULLY create a command function and a predicate function during today's class.

 

═══════════════════════════

WORDS O' THE DAY: Parameter

 

WORK O' THE DAY:

Please write a function that:

1) accepts an integer number as an argument

2) Says:

a) "You chose a triangle" when the argument = 3

b) "You chose a square" when the argument = 4

c) "You chose a circle" when the argument = 5

d) "You made an improper choice, please try again" when the argument = 6

3) Does NOT return any values.

═══════════════════════════

Now let's spend some time talking about those pesky arguments (or parameters if you like, either name will do)

Pretty much all computer languages allow you to SEND IN as many arguments as you like.

However, you can ONLY return one value back to the calling program. You can, however, make changes to the arguments inside your function, but we'll talk about that another day.

Take a look at this code:

  1. What kind of function is this? How do you know?

  2. Now please suggest what the function (heh heh!) of this function might be

  3. What could/should be better (let's say you just inherited this function on the job from someone who got hurt by falling space junk)?

═══════════════════════════

 

 

HINT: We will begin commenting our code (see below) starting today!

It might be helpful to know that "concatenate" means to join separate variables into one longer 'string' of terms.

When would a function like this be really, really helpful?

I made a wee change between the two screen shots... can you find it?

Why did I do that?

There is a way to actually send SNAP code to each other via text files. It can be a bit odd but see if you can import this function(xml) or HERE (shared) into your own file and write a program that successfully CALLS that function.

Fix any bugs you find but DO NOT make substantial changes to the function